var prefix = ctx + "manage/proxyLevel"

$(function () {
    var columns = [{
        checkbox: true
    },
        {
            field: 'id',
            title: '主键'
        },
        {
            field: 'plStatus',
            title: '开关',
            formatter: function (value, row, index) {
                if (value === 1) {
                    return "   <div class=\"onoffswitch\">\n" +
                        "      <input type=\"checkbox\" onchange=\"checkStatus("+row.id+")\" class=\"onoffswitch-checkbox btn-embossed btn\" id=\"status"+row.id+"\" name=\"status"+row.id+"\" checked=\"checked\">\n" +
                        "      <label class=\"onoffswitch-label\" for=\"status"+row.id+"\"> <span class=\"onoffswitch-inner\"></span>\n" +
                        "          <span class=\"onoffswitch-switch\"></span>\n" +
                        "      </label>\n" +
                        "  </div>";
                }
                if (value === 2) {
                    return "   <div class=\"onoffswitch\">\n" +
                        "      <input type=\"checkbox\" onchange=\"checkStatus("+row.id+")\" class=\"onoffswitch-checkbox btn-embossed btn\" id=\"status"+row.id+"\" name=\"status"+row.id+"\">\n" +
                        "      <label class=\"onoffswitch-label\" for=\"status"+row.id+"\"> <span class=\"onoffswitch-inner\"></span>\n" +
                        "          <span class=\"onoffswitch-switch\"></span>\n" +
                        "      </label>\n" +
                        "  </div>";
                }

            }
        },
        {
            field: 'plName',
            title: '名称'
        },
        {
            field: 'plEffective',
            title: '代理比例'
        },

        {
            field: 'createTime',
            title: '创建时间'
        },
        {
            title: '操作',
            align: 'center',
            formatter: function (value, row, index) {
                var actions = [];
                actions.push('<a class="btn btn-primary btn-sm ' + editFlag + '" href="#" title="编辑" mce_href="#" onclick="edit(\'' + row.id + '\')"><i class="fa fa-edit"></i></a> ');
                actions.push('<a class="btn btn-warning btn-sm ' + removeFlag + '" href="#" title="删除" onclick="remove(\'' + row.id + '\')"><i class="fa fa-remove"></i></a>');
                return actions.join('');
            }
        }];
    var url = prefix + "/list";
    $.initTable(columns, url);
});

/*代理等级-新增*/
function add() {
    var url = prefix + '/add';
    layer_showAuto("新增代理等级", url);
}

/*代理等级-修改*/
function edit(id) {
    var url = prefix + '/edit/' + id;
    layer_showAuto("修改代理等级", url);
}

// 单条删除
function remove(id) {
    $.modalConfirm("确定要删除选中代理等级吗？", function () {
        _ajax(prefix + "/remove/" + id, "", "post");
    })
}

// 批量删除
function batchRemove() {
    var rows = $.getSelections("id");
    if (rows.length == 0) {
        $.modalMsg("请选择要删除的数据", "warning");
        return;
    }
    $.modalConfirm("确认要删除选中的" + rows.length + "条数据吗?", function () {
        _ajax(prefix + '/batchRemove', {"ids": rows}, "post");
    });
}

function checkStatus(id) {
    var checkStatus = $("#status"+id);
    var plStatus;
    if (checkStatus.is(":checked")) {
        plStatus = 1;
    } else {
        plStatus  = 2;
    }
    _ajax_query(ctx + "manage/proxyLevel/save", {id:id,plStatus:plStatus}, function (result) {
        if (result.code == web_status.SUCCESS) {
            $.modalMsg(result.msg, modal_status.SUCCESS);
        } else {
            $.modalAlert(result.msg, modal_status.FAIL);
        }
    });
}
